<?php

class sms_controller_announcement extends Controller{
	/**
	 *
	 * @var sms_model_announcement
	 */
	var $a;
	/**
	 *
	 * @var sms_model_annlog
	 */
	var $l;
	
	const PAGE_SIZE = 25;
	
	public function __construct($app, $controller){
		parent::__construct($app, $controller);
		$this->a = Model::factory("sms_model_announcement");
		$this->l = Model::factory("sms_model_annlog");
		$this->data = Model::factory('sms_model_data');
		$this->headerView = $this->viewDir."/_header.phtml";
		$this->__title = '消息中心';
	}
	
	public function view_inbox_action(){
		$this->requireLogin();
		$page = _('page', 1);
		$ret = CacheManager::getInstance()->get(array(
			'app'=>$this->app,
			'func'=>array(
				array($this->a, 'selectPage'), 
				array($page, self::PAGE_SIZE )				
			)
		), true);
		
		if($ret){
			$uid = self::$login['uid'];
			$pages = $this->genPages($ret['INFO']);
			$this->ret = self::success(array('ret'=>$ret, 'pages'=>$pages));
			$newNotice = $this->data->queryUnReadSms($uid,false);
			if(($anno = MemRow::getCache(CACHE_KEY_SYS_ANNO)) && $anno['time']>$newNotice['announcement']){
				$this->data->updateSmsCount($uid,sms_config::MSG_SYS_ANNO_CNT,time());
			}
			
		}else{
			$this->ret = self::failure(Error::ERROR_UNKNOWN );
		}
//		$this->data->updateSmsCount(self::$login['uid'],);
		$this->view = 'inbox';
	}
	
	public function add_action(){
		$this->_renderJson();
		$params = array(
			'title'=>_('title'),
			'content'=>_('content'),
			'time'=>_('time')
		);
		trim_params($params);
		extract($params, EXTR_SKIP);
		
		$checksum = _('checksum');
		if(sms_util_verifier::check($params, $checksum)){
			$this->ret = self::failure(Error::ERROR_CHECKSUM );
			return;
		}
		if(abs($time - time())>600){
			$this->ret = self::failure(Error::ERROR_EXPIRE );
			return;
		}
		$id = $this->a->add($params);
		if($id){
			$this->ret = self::success($id);
		}else{
			$this->ret = self::failure(Error::ERROR_UNKNOWN );
		}
	}
	
	public function update_action(){
		$this->_renderJson();
		
		$params = array(
			'id'=>_('id'),
			'title'=>_('title'),
			'content'=>_('content'),
			'time'=>_('time')
		);
		
		trim_params($params);
		extract($params, EXTR_SKIP);
		
		$checksum = _('checksum');
		if(sms_util_verifier::check($params, $checksum)){
			$this->ret = self::failure(Error::ERROR_CHECKSUM );
			return;
		}
		if(abs($time - time())>600){
			$this->ret = self::failure(Error::ERROR_EXPIRE );
			return;
		}
		
		$flag = $this->a->update($params);
		if($flag){
			$this->ret = self::success($flag);
		}else{
			$this->ret = self::failure(Error::ERROR_UNKNOWN );
		}
	}
	
	public function delete_action(){
		$this->_renderJson();
		
		$params = array(
			'id'=>_('id'),
			'time'=>_('time')
		);
		
		trim_params($params);
		extract($params, EXTR_SKIP);
		
		$checksum = _('checksum');
		if(sms_util_verifier::check($params, $checksum)){
			$this->ret = self::failure(Error::ERROR_CHECKSUM );
			return;
		}
		if(abs($time - time())>600){
			$this->ret = self::failure(Error::ERROR_EXPIRE );
			return;
		}
		
		$flag = $this->a->delete($params);
		if($flag){
			$this->ret = self::success($flag);
		}else{
			$this->ret = self::failure(Error::ERROR_UNKNOWN );
		}
	}
}